import re


a = "we are students"

# match
result = re.match('(.*) (.*) (.*)', a)  # (.*) 后面有空格，match 只匹配字符串开始
print(result.span())  # (0, 15) 以 tuple 形式返回范围
print(result.start())  # 0 开始位置
print(result.end())  # 15 结束位置

print(result.groups())  # ('we', 'are', 'students') ，返回匹配上的三个元组
print(result.group())  # we are students 匹配上的整个字符串
print(result.group(0))  # we are students 匹配上的整个字符串

print(result.group(1))  # we
print(result.group(2))  # are
print(result.group(3))  # students
# print(result.group(4))  # IndexError: no such group

# search
result = re.match('(a.*)', a)  # 只匹配字符串开始
print(result)  # None
result = re.search('(a.*)', a)  # 只匹配字符串开始
print(result)  # <re.Match object; span=(3, 15), match='are students'>
print(result.group())  # are students

msg = '''I am good
Why you think 123 is a number'''

msg1 = '''Why you think 
®123
 is a number'''

result = re.match('Why you think.*?(\d+).*', msg1)
print(result)  # None ，match 从开头开始匹配
result = re.search('Why you think.*?(\d+).*', msg)
print(result.group(1))  # 123，可以匹配换行符
result = re.findall('Why you think.*?(\d+).*', msg)
print(result)  # ['123']，可以匹配换行符
